<template>
  <div class="operation">
    <div class="top current-header">
      <header>
        <h3><span class="tip"></span> 盘点管理</h3>
        <div class="operation-search">
          <a-form layout="inline">
            <a-form-item label="计划盘点时间">
              <a-range-picker
                format="YYYY-MM-DD"
                @change="onTime"
                v-model="defaulttimeList"
              >
                <template slot="dateRender" slot-scope="current">
                  <div
                    class="ant-calendar-date"
                    :style="getcurrentStyle(current)"
                  >
                    {{ current.date() }}
                  </div>
                </template>
              </a-range-picker>
            </a-form-item>
            <a-form-item label="盘点单号">
              <a-input v-model="searchItem.inventoryOrderNo" />
            </a-form-item>

            <a-form-item label="厂区">
              <a-select
                class="selectwidth"
                v-model="searchItem.wsType"
                placeholder="请选择"
                @change="handleChangeWSType"
                style="width: 150px"
              >
                <a-select-option
                  v-for="item in WSTypeList"
                  :key="item.type"
                  :value="item.type"
                  >{{ item.name }}</a-select-option
                >
              </a-select>
            </a-form-item>
            <a-form-item label="站点">
              <a-select
                class="selectwidth"
                v-model="searchItem.wsSegmentID"
                placeholder="请选择"
                style="width: 200px"
              >
                <a-select-option
                  v-for="item in QSegmentList"
                  :key="item.type"
                  :value="item.type"
                  >{{ item.name }}</a-select-option
                >
              </a-select>
            </a-form-item>

            <a-form-item>
              <a-button class="default-button" @click="getData">
                查询
              </a-button>
            </a-form-item>
            <a-form-item>
              <a-button class="default-button" @click="addFun"> 添加 </a-button>
            </a-form-item>
            <a-form-item>
              <general-button
                :btn-text="
                  currentRow.Status == 1
                    ? '审核'
                    : currentRow.Status == 2
                    ? '弃审'
                    : '弃审'
                "
                :disabled="
                  currentRow.Status == 3 ||
                  currentRow.InventoryOrderID == null ||
                  currentRow.Status == 4
                "
                btnPowerNum="D99B0601-579D-4A8C-8B46-75673074ED58"
                @operation="reviewFun"
              ></general-button>
            </a-form-item>
            <a-form-item>
              <general-button
                btn-text="下发"
                :disabled="
                  currentRow.InventoryOrderID == null ||
                  currentRow.Status == 4 ||
                  currentRow.Status == 1
                "
                btnPowerNum="34D61AD5-F594-4FAC-BBF9-8307A1FCE43D"
                @operation="issuedFun"
              ></general-button>
            </a-form-item>
            <a-form-item>
              <general-button
                btn-text="关闭"
                :disabled="
                  currentRow.InventoryOrderID == null || currentRow.Status == 4
                "
                btnPowerNum="5A4E178A-FDA8-4C77-9027-FDC6FCD83A57"
                @operation="closeFun"
              ></general-button>
            </a-form-item>
          </a-form>
        </div>
      </header>
      <section id="TableBasicBox">
        <div class="table-basic-box">
          <a-table
            className="components-table-demo-nested"
            :columns="topColumns"
            :row-key="(record) => record.InventoryOrderID"
            :data-source="dataPage.Data"
            :loading="dataPage.loading"
            :customRow="customRow"
            :scroll="{ y: topTable }"
            :pagination="false"
            size="middle"
          >
            <template slot="radioCheck" slot-scope="text">
              <a-radio-group v-model="currentRow.InventoryOrderID">
                <a-radio :value="text"></a-radio>
              </a-radio-group>
            </template>
            <template slot="action" slot-scope="text, record">
              <div class="chart-manage">
                <a-tag color="#008000" @click="editFun(record)"> 修改 </a-tag>
                <a-tag color="#ff0000" @click="delFun(record)"> 删除 </a-tag>
              </div>
            </template>
          </a-table>
          <div class="table-paging">
            <a-pagination
              show-size-changer
              :default-current="1"
              :pageSizeOptions="pageSizeOptions"
              :total="dataPage.TotalNumber"
              @change="onChange"
              @showSizeChange="onShowsizeChange"
            />
          </div>
        </div>
      </section>
    </div>
    <div class="bottom current-header">
      <header>
        <ul class="tab-box">
          <li
            v-for="item in activeList"
            :key="item.value"
            :class="[activeValue == item.value ? 'active-class' : '']"
            @click="switchFun(item.value)"
          >
            {{ item.name }}
          </li>
        </ul>

        <div class="operation-search">
          <a-form v-if="activeValue == 0" layout="inline">
            <a-form-item label="精华液编码/名称">
              <a-input
                v-model="inventoryDetailsSearch.materialCodeName"
                allowClear
              />
            </a-form-item>
            <a-form-item label="盘点状态">
              <a-select
                :allowClear="true"
                mode="multiple"
                v-model="inventoryDetailsSearch.confirmStatusList"
                style="width: 150px"
              >
                <a-select-option
                  v-for="item in confirmStatusList"
                  :key="item.value"
                  :value="item.value"
                >
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item>
              <a-button class="default-button" @click="getDetailDataFun(true)">
                查询
              </a-button>
            </a-form-item>
            <a-form-item>
              <general-button
                btn-text="添加明细"
                :disabled="
                  currentRow.InventoryOrderID == null ||
                  currentRow.Status == 4 ||
                  currentRow.Status == 0 ||
                  !currentRow.Status
                "
                btnPowerNum="4D464589-865D-4061-959C-EA22FA8FD245"
                @operation="addDetailsFun"
              ></general-button>
            </a-form-item>
            <a-form-item>
              <general-button
                btn-text="完成盘点"
                :disabled="materialDetailsPage.rowKey.length == 0"
                btnPowerNum="CFCD1968-3AAD-4D42-A417-8DF01271F330"
                @operation="physicalCount"
              ></general-button>
            </a-form-item>
            <a-form-item>
              <general-button
                btn-text="放弃盘点"
                :disabled="materialDetailsPage.rowKey.length == 0"
                btnPowerNum="CA6D1B84-6155-483F-B44C-A447512CD5A9"
                @operation="abandoningInventory"
              ></general-button>
            </a-form-item>
          </a-form>

          <a-form v-else layout="inline">
            <a-form-item label="精华液编码/名称">
              <a-input v-model="inventoryRecordSearch.materialCodeName" />
            </a-form-item>
            <a-form-item label="精华液条码">
              <a-input v-model="inventoryRecordSearch.materialBarcode" />
            </a-form-item>
            <a-form-item label="盘点状态">
              <a-select
                :allowClear="true"
                mode="multiple"
                v-model="inventoryRecordSearch.inventoryStatusList"
                style="width: 150px"
              >
                <a-select-option
                  v-for="item in inventoryStatusList"
                  :key="item.value"
                  :value="item.value"
                >
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item>
              <a-button
                class="default-button"
                @click="getInventoryRecordFun(true)"
              >
                查询
              </a-button>
            </a-form-item>
          </a-form>
        </div>
      </header>
      <section id="ExtattrbuteTableBox">
        <div class="table-basic-box" v-if="activeValue == 0" :key="1">
          <a-table
            className="components-table-demo-nested"
            :columns="detailColumns"
            :row-key="(record) => record.num"
            :data-source="materialDetailsPage.Data"
            :loading="materialDetailsPage.loading"
            :row-selection="{
              selectedRows: materialDetailsPage.rowKey,
              onChange: materialDetailChangeFun,
            }"
            :scroll="{ y: bottomTable }"
            :pagination="false"
            size="middle"
          >
            <template slot="action" slot-scope="text, record">
              <div class="chart-manage">
                <a-tag color="#ff0000" @click="delDetailsFun(record)">
                  删除
                </a-tag>
              </div>
            </template>
          </a-table>
          <div class="table-paging">
            <a-pagination
              :current="inventoryDetailsSearch.pageRequestModel.pageIndex"
              show-size-changer
              :pageSize="inventoryDetailsSearch.pageRequestModel.pageSize"
              :default-current="1"
              :total="materialDetailsPage.TotalNumber"
              :pageSizeOptions="pageSizeOptions"
              @change="onDetailsChange"
              @showSizeChange="onDetailsShowsizeChange"
            />
          </div>
        </div>

        <div class="table-basic-box" v-if="activeValue == 1" :key="2">
          <a-table
            className="components-table-demo-nested"
            :columns="recordColumns"
            :row-key="(record) => record.num"
            :data-source="recordPage.Data"
            :loading="recordPage.loading"
            :scroll="{ y: bottomTable }"
            :pagination="false"
            size="middle"
          >
          </a-table>
          <div class="table-paging">
            <a-pagination
              :current="inventoryRecordSearch.pageRequestModel.pageIndex"
              :pageSize="inventoryRecordSearch.pageRequestModel.pageSize"
              show-size-changer
              :default-current="1"
              :total="recordPage.TotalNumber"
              :pageSizeOptions="pageSizeOptions"
              @change="onRecordChange"
              @showSizeChange="onRecordShowSizeChange"
            />
          </div>
        </div>
      </section>
    </div>

    <stocktaking-form
      v-model="topAddShow"
      :essence-item="essenceItem"
      :is-edit="dataPage.isEdit"
      @up-data="
        () => {
          this.currentRow = {};
          this.getData();
        }
      "
    ></stocktaking-form>

    <material-form
      v-model="materialShow"
      :materialItem="currentRow"
      @upData="getDetailDataFun"
    ></material-form>
  </div>
</template>
<script>
import { GetWorkShopTypesAll } from "@/api/WorkShopType/index";
import { GetSegments } from "@/api/NBCSegment/index";

import {
  getInventoryOrderList,
  reviewInventoryOrder,
  issueInventoryOrder,
  getInventoryCodeList,
  delInventoryCode,
  delInventoryOrder,
  giveUpInventoryCode,
  closeInventoryOrder,
  completeInventoryCode,
  getInventoryRecordList,
} from "@/api/InventoryOrder/index";

import GeneralButton from "@/components/be-current-btn.vue";
import stocktakingForm from "./children/stocktaking-form.vue";
import MaterialForm from "./children/material-details-form.vue";
import moment from "moment";
import { getTableScroll } from "@/common/table-calc";
const topColumns = [
  {
    title: "单选",
    dataIndex: "InventoryOrderID",
    scopedSlots: { customRender: "radioCheck" },
    fixed: "left",
    align: "center",
    width: 50,
  },
  {
    title: "序号",
    dataIndex: "num",
    key: "num",
    align: "center",
    width: "20%",
  },
  {
    title: "盘点单号",
    dataIndex: "InventoryOrderNo",
    key: "InventoryOrderNo",
    align: "center",
    width: "40%",
  },
  {
    title: "计划盘点时间",
    dataIndex: "PlanDtName",
    key: "PlanDtName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "厂区",
    dataIndex: "WSTypeName",
    key: "WSTypeName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "站点",
    dataIndex: "WSSegmentName",
    key: "WSSegmentName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "状态",
    dataIndex: "StatusName",
    key: "StatusName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "创建人",
    dataIndex: "CreateUserName",
    key: "CreateUserName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "创建时间",
    dataIndex: "CreateDtName",
    key: "CreateDtName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "修改人",
    dataIndex: "ModifyUserName",
    key: "ModifyUserName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },

  {
    title: "修改时间",
    dataIndex: "ModifyDtName",
    key: "ModifyDtName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "关闭人",
    dataIndex: "CloseUserName",
    key: "CloseUserName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "关闭时间",
    dataIndex: "CloseDtName",
    key: "CloseDtName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "操作",
    dataIndex: "operate",
    width: "30%",
    align: "center",
    scopedSlots: { customRender: "action" },
  },
]; //盘点单表格表头

const detailColumns = [
  {
    title: "序号",
    dataIndex: "num",
    key: "num",
    align: "center",
    width: "10%",
  },
  {
    title: "物料编码",
    dataIndex: "MaterialCode",
    key: "MaterialCode",
    align: "center",
    width: "40%",
  },
  {
    title: "物料名称",
    dataIndex: "MaterialName",
    key: "MaterialName",
    align: "center",
    width: "40%",
  },
  {
    title: "状态",
    dataIndex: "ConfirmStatusName",
    key: "ConfirmStatusName",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "盘点前数量",
    dataIndex: "BefNum",
    key: "BefNum",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "盘点后数量",
    dataIndex: "AftNum",
    key: "AftNum",
    ellipsis: true,
    align: "center",
    width: "40%",
  },
  {
    title: "操作",
    dataIndex: "operate",
    width: "30%",
    align: "center",
    scopedSlots: { customRender: "action" },
  },
]; //盘点单物料表格表头

const recordColumns = [
  {
    title: "序号",
    dataIndex: "num",
    key: "num",
    align: "center",
    width: "5%",
  },
  {
    title: "物料条码",
    dataIndex: "MaterialBarcode",
    key: "MaterialBarcode",
    align: "center",
    width: "10%",
  },
  {
    title: "物料编码",
    dataIndex: "MaterialCode",
    key: "MaterialCode",
    align: "center",
    width: "10%",
  },
  {
    title: "物料名称",
    dataIndex: "MaterialName",
    key: "MaterialName",
    align: "center",
    width: "10%",
  },
  {
    title: "批次号",
    dataIndex: "BatchNo",
    key: "BatchNo",
    align: "center",
    width: "10%",
  },
  {
    title: "盘点前状态",
    dataIndex: "BefStatus",
    key: "BefStatus",
    align: "center",
    width: "5%",
  },
  {
    title: "盘点前数量",
    dataIndex: "BefNum",
    key: "BefNum",
    align: "center",
    width: "5%",
  },
  {
    title: "盘点后数量",
    dataIndex: "AftNum",
    key: "AftNum",
    align: "center",
    width: "5%",
  },
  {
    title: "差异数量",
    dataIndex: "WeightDifference",
    key: "WeightDifference",
    align: "center",
    width: "5%",
  },
  {
    title: "是否计划内",
    dataIndex: "IsInnerItem",
    key: "IsInnerItem",
    align: "center",
    width: "5%",
  },
  {
    title: "盘点状态",
    dataIndex: "Result",
    key: "Result",
    align: "center",
    width: "10%",
  },
  {
    title: "盘点人",
    dataIndex: "OperateUserName",
    key: "OperateUserName",
    align: "center",
    width: "10%",
  },
  {
    title: "盘点时间",
    dataIndex: "OperateDt",
    key: "OperateDt",
    align: "center",
    width: "10%",
  },
];
export default {
  name: "InventoryManagement",
  components: {
    GeneralButton,
    stocktakingForm,
    MaterialForm,
  },
  data() {
    return {
      searchItem: {
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 10,
        },
        planDtStartDt: "",
        planDtEndDt: "",
        inventoryOrderNo: "",
        wsType: null,
        wsSegmentID: null,
      }, //搜索参数
      isFilterShow: false, //更多筛选显示
      topRowKey: [], //选中值
      topRow: {}, //选中对象
      topColumns, //盘点单表头
      topAddShow: false, //盘点单添加
      essenceItem: {}, //盘点单添加对象
      defaulttimeList: [], //选择时间数组
      topTable: 0, //顶部表格高度
      bottomTable: 0, //底部表格高度

      dataPage: {
        Data: [],
        pageIndex: 1,
        pageSize: 10,
        totalNumber: 0,
        totalPage: 0,
        isEdit: false, //是否修改盘点单
        loading: false, //表格loading
      },
      confirmStatusList: [
        { label: "待完成", value: 0 },
        { label: "已完成", value: 1 },
        { label: "已放弃", value: 2 },
      ], //状态列表
      inventoryStatusList: [
        { label: "待盘点", value: 0 },
        { label: "已扫码确认", value: 1 },
        { label: "盘点解冻", value: 2 },
        { label: "盘点注销", value: 3 },
      ], //状态列表
      currentRow: { InventoryOrderID: null }, //当前盘点单
      inventoryDetailsSearch: {
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 10,
        },
        inventoryOrderID: null,
        materialCodeName: null,
        confirmStatusList: [],
      }, //明细查询信息
      detailColumns, //明细表头
      pageSizeOptions: ["10", "50", "100", "500", "1000"], //分页
      materialDetailsPage: {
        Data: [],
        PageIndex: 1,
        PageSize: 10,
        TotalNumber: 0,
        TotalPage: 0,
        isEdit: false, //是否修改明细
        loading: false, //表格loading
        isShow: false,
        rowKey: [],
        item: {}, //添加的明细对象
      }, //当前明细
      currentDetailPage: {}, //当前明细
      assignedPage: {
        isShow: false,
      }, //已分配
      WSTypeList: [], //厂区列表
      QSegmentList: [], //站点列表
      activeValue: 0, //当前选择是明细还是记录
      activeList: [
        { name: "盘点明细", value: 0 },
        { name: "盘点记录", value: 1 },
      ],
      materialShow: false, //物料添加显示

      inventoryRecordSearch: {
        pageRequestModel: {
          pageIndex: 1,
          pageSize: 10,
        },
        inventoryOrderID: null,
        materialCodeName: null,
        inventoryStatusList: [],
        materialBarcode: null,
      }, //明细记录查询信息
      recordPage: {
        Data: [],
        PageIndex: 1,
        PageSize: 10,
        TotalNumber: 0,
        TotalPage: 0,
      }, //盘点记录参数
      recordColumns, //盘点记录表头
    };
  },

  methods: {
    /** 切换tab项 */
    switchFun(value) {
      this.activeValue = value;
    },

    /** 选中厂区 */
    handleChangeWSType(value) {
      let that = this;
      if (value) {
        that.GetSegmentsAllFun(that.searchItem.WSTypeName, true);
      }
    },
    /** 获取厂区列表 */
    async GetWorkShopTypesAllFun() {
      let that = this;
      let res = await GetWorkShopTypesAll(10);
      if (res.Status == 10000) {
        let wtList = [];
        res.Data.map((item) => {
          wtList.push({ type: item.WSType, name: item.WSName });
        });
        that.WSTypeList = wtList;
      } else {
        that.$message.error(res.Message);
      }
    },

    //获取车间列表
    async GetSegmentsAllFun(WSType) {
      let that = this;
      let SegSegmentID = [
        "67CE6CD3-0E52-4284-AAFC-E20869415EAA",
        "AE6D566D-D22F-4280-9A14-9375B915AFD0",
      ];
      let res = await GetSegments({
        SegSegmentID: SegSegmentID,
        WSTypeName: WSType,
      });
      if (res.Status == 10000) {
        let wtList = [];
        res.Data.map((item) => {
          wtList.push({ type: item.SegmentID, name: item.SegmentName });
        });
        that.QSegmentList = wtList;
      } else {
        that.$message.error(res.Message);
      }
      that.spinning = false;
    },

    /** 点击查询明细 */
    customRow(record, index) {
      let that = this;
      return {
        style: {
          "background-color":
            record.InventoryOrderID === that.currentRow?.InventoryOrderID
              ? "#E6F7FF"
              : "",
        },
        on: {
          // 鼠标单击行
          click: (event) => {
            // 记录当前点击的行标识
            that.currentRow = record;
            that.getDetailDataFun(true);
            that.getInventoryRecordFun(true);
          },
        },
      };
    },

    /** 查询盘点单
     * @flag flase为重置查询 true为下一页查询
     */
    async getData(flag) {
      if (!flag) {
        this.searchItem.pageRequestModel.pageIndex = 1;
        this.searchItem.pageRequestModel.pageSize = 10;
        this.dataPage.TotalNumber = 0;
      }

      try {
        let { Data } = await getInventoryOrderList(this.searchItem);
        let { pageIndex, pageSize } = this.searchItem.pageRequestModel;
        Data.Data.map((item, index) => {
          item.num = (pageIndex - 1) * pageSize + index + 1;
          item.CloseDtName = item.CloseDt
            ? moment(item.CloseDt).format("YYYY-MM-DD HH:mm:ss")
            : "";
          item.PlanDtName = item.PlanDt
            ? moment(item.PlanDt).format("YYYY-MM-DD HH:mm:ss")
            : "";
          item.ModifyDtName = item.ModifyDt
            ? moment(item.ModifyDt).format("YYYY-MM-DD HH:mm:ss")
            : "";
          item.CreateDtName = item.CreateDt
            ? moment(item.CreateDt).format("YYYY-MM-DD HH:mm:ss")
            : "";
        });
        this.dataPage = Data;
        this.currentRow = { InventoryOrderID: null };
        this.materialDetailsPage = {
          Data: [],
          PageIndex: 1,
          PageSize: 10,
          TotalNumber: 0,
          TotalPage: 0,
          isEdit: false, //是否修改明细
          loading: false, //表格loading
          isShow: false,
          rowKey: [],
          item: {}, //添加的明细对象
        };
        this.recordPage = {
          Data: [],
          PageIndex: 1,
          PageSize: 10,
          TotalNumber: 0,
          TotalPage: 0,
        };
      } catch (e) {
        this.$message.error(e);
      }
    },

    /** 批量删除盘点单 */
    async delDatas() {
      let that = this;
      this.$confirm({
        title: "提示",
        content: `是否确认删除选中${that.topRow.length}项`,
        async onOk() {
          try {
            let { Data } = await delSemiMGoodsDistributeList(that.topRow);
            that.$message.success("删除成功");
            that.getData();
          } catch (e) {
            that.$message.error(e);
          }
        },
        onCancel() {},
      });
    },

    /** 时间选择器的样式填充 */
    getcurrentStyle(current, today) {
      const style = {};
      if (current.date() === 1) {
        style.border = "1px solid #1890ff";
        style.borderRadius = "50%";
      }
      return style;
    },
    /** 选中项 */
    topTableChangeFun(selectedRowKeys, selectedRows) {
      this.topRow = selectedRows; //选中项信息
      this.topRowKey = selectedRowKeys; //选中项ID
    },

    /** 添加盘点单 */
    addFun() {
      this.essenceItem = {
        InventoryOrderNo: null,
        PlanDt: null,
        WSType: null,
        WSSegmentID: null,
        Remark: null,
      };
      this.topAddShow = true;
      this.dataPage.isEdit = false;
    },
    /** 修改盘点单 */
    editFun(item) {
      this.essenceItem = JSON.parse(JSON.stringify(item));
      this.topAddShow = true;
      this.dataPage.isEdit = true;
    },
    /** 删除盘点单 */
    async delFun(item) {
      let that = this;
      this.$confirm({
        title: "提示",
        content: `删除盘点单【${item.InventoryOrderNo}】项`,
        async onOk() {
          let res = await delInventoryOrder(item);
          if (res.Status == 10000) {
            that.$message.success("删除成功");
            that.getData();
          }
        },
        onCancel() {},
      });
    },
    /** 审核盘点单 */
    reviewFun() {
      let that = this;
      if (!that.currentRow || !that.currentRow.InventoryOrderNo) {
        that.$message.error("请选择盘点单");
        return;
      }

      this.$confirm({
        title: "提示",
        content: `${that.currentRow.Status == 1 ? "审核" : "弃审"}盘点单【${
          that.currentRow.InventoryOrderNo
        }】项`,
        async onOk() {
          let res = await reviewInventoryOrder(that.currentRow);
          if (res.Status == 10000) {
            that.$message.success("处理成功");
            that.getData();
          }
        },
        onCancel() {},
      });
    },

    /** 下发盘点单 */
    issuedFun() {
      let that = this;
      this.$confirm({
        title: "提示",
        content: `下发盘点单【${that.currentRow.InventoryOrderNo}】`,
        async onOk() {
          let res = await issueInventoryOrder(that.currentRow);
          if (res.Status == 10000) {
            that.$message.success("处理成功");
            that.getData();
          }
        },
        onCancel() {},
      });
    },
    /** 关闭盘点单 */
    closeFun() {
      let that = this;
      this.$confirm({
        title: "提示",
        content: `关闭盘点单【${that.currentRow.InventoryOrderNo}】`,
        async onOk() {
          let res = await closeInventoryOrder(that.currentRow);
          if (res.Status == 10000) {
            that.$message.success("处理成功");
            that.getData();
          }
        },
        onCancel() {},
      });
    },

    /** 查询盘点单明细 */
    async getDetailDataFun(flag) {
      this.inventoryDetailsSearch.inventoryOrderID = [
        this.currentRow.InventoryOrderID,
      ];
      if (flag) {
        this.inventoryDetailsSearch.pageRequestModel.pageIndex = 1;
        this.inventoryDetailsSearch.pageRequestModel.pageSize = 10;
        this.materialDetailsPage.Data = [];
      }
      try {
        let { Data } = await getInventoryCodeList(this.inventoryDetailsSearch);
        let { pageIndex, pageSize } =
          this.inventoryDetailsSearch.pageRequestModel;
        Data.Data.map((item, index) => {
          item.num = (pageIndex - 1) * pageSize + index + 1;
          item.ModifyDtName = item.ModifyDt
            ? moment(item.ModifyDt).format("YYYY-MM-DD HH:mm:ss")
            : "";
          item.CreateDtName = item.CreateDt
            ? moment(item.CreateDt).format("YYYY-MM-DD HH:mm:ss")
            : "";
        });
        this.materialDetailsPage = {
          ...this.materialDetailsPage,
          ...Data,
        };
      } catch (e) {
        this.$message.error(e);
      }
    },

    /** 删除盘点单明细 */
    delDetailsFun(item) {
      let that = this;
      this.$confirm({
        title: "提示",
        content: `删除盘点单明细【${item.MaterialCode}(${item.MaterialName})】`,
        async onOk() {
          let res = await delInventoryCode([item]);
          if (res.Status == 10000) {
            that.$message.success("删除成功");
            that.getDetailDataFun();
          }
        },
        onCancel() {},
      });
    },

    /** 新增盘点单明细 */
    addDetailsFun() {
      this.materialShow = true;
    },

    /** 盘点单选中 */
    materialDetailChangeFun(selectedRowKeys, selectedRows) {
      this.materialDetailsPage.rowKey = selectedRows;
    },

    /** 盘点单明细完成盘点 */
    physicalCount() {
      let that = this;
      this.$confirm({
        title: "提示",
        content: `完成${that.materialDetailsPage.rowKey.length}项盘点`,
        async onOk() {
          let res = await completeInventoryCode(
            that.materialDetailsPage.rowKey
          );
          if (res.Status == 10000) {
            that.$message.success("成功");
            that.getDetailDataFun();
          }
        },
        onCancel() {},
      });
    },

    /** 盘点单明细放弃盘点 */
    abandoningInventory() {
      let that = this;
      this.$confirm({
        title: "提示",
        content: `放弃${that.materialDetailsPage.rowKey.length}项盘点`,
        async onOk() {
          let res = await giveUpInventoryCode(that.materialDetailsPage.rowKey);
          if (res.Status == 10000) {
            that.$message.success("成功");
            that.getDetailDataFun();
          }
        },
        onCancel() {},
      });
    },

    /** 筛选选择时间 */
    onTime(e) {
      let that = this;
      that.searchItem.planDtStartDt = that.dateExt.showDateTime(e[0]);
      that.searchItem.planDtEndDt = that.dateExt.showDateTime(e[1]);
    },

    /** 查询盘点单记录 */
    async getInventoryRecordFun(flag) {
      this.inventoryRecordSearch.inventoryOrderID = [
        this.currentRow.InventoryOrderID,
      ];
      if (flag) {
        this.inventoryRecordSearch.pageRequestModel.pageIndex = 1;
        this.inventoryRecordSearch.pageRequestModel.pageSize = 10;
        this.recordPage.Data = [];
      }
      try {
        let { Data } = await getInventoryRecordList(this.inventoryRecordSearch);
        let { pageIndex, pageSize } =
          this.inventoryRecordSearch.pageRequestModel;
        Data.Data.map((item, index) => {
          item.num = (pageIndex - 1) * pageSize + index + 1;
          item.OperateDt = item.OperateDt
            ? moment(item.OperateDt).format("YYYY-MM-DD HH:mm:ss")
            : "";
          item.BefStatus =
            item.BefStatus == 2
              ? "暂存站内"
              : item.BefStatus == 21
              ? "静置站内"
              : "";

          item.IsInnerItem = item.IsInnerItem ? "是" : "否";
        });
        this.recordPage = {
          ...this.recordPage,
          ...Data,
        };
      } catch (e) {
        this.$message.error(e);
      }
    },

    /** 分页选择 */
    onChange(page, pageSize) {
      this.searchItem.pageRequestModel.pageIndex = page;
      this.searchItem.pageRequestModel.pageSize = pageSize;
      this.getData(true);
    },

    /** 条数选择 */
    onShowsizeChange(current, size) {
      this.searchItem.pageRequestModel.pageIndex = current;
      this.searchItem.pageRequestModel.pageSize = size;
      this.getData(true);
    },

    /**  明细分页选择 */
    onDetailsChange(page, pageSize) {
      this.inventoryDetailsSearch.pageRequestModel.pageIndex = page;
      this.inventoryDetailsSearch.pageRequestModel.pageSize = pageSize;
      this.getDetailDataFun();
    },

    /**  明细条数选择 */
    onDetailsShowsizeChange(current, size) {
      this.inventoryDetailsSearch.pageRequestModel.pageIndex = current;
      this.inventoryDetailsSearch.pageRequestModel.pageSize = size;
      this.getDetailDataFun();
    },

    /** 盘点记录分页选择 */
    onRecordChange(page, pageSize) {
      this.inventoryRecordSearch.pageRequestModel.pageIndex = page;
      this.inventoryRecordSearch.pageRequestModel.pageSize = pageSize;
      this.getInventoryRecordFun();
    },

    /** 盘点记录条数选择  */
    onRecordShowSizeChange(current, size) {
      this.inventoryRecordSearch.pageRequestModel.pageIndex = current;
      this.inventoryRecordSearch.pageRequestModel.pageSize = size;
      this.getInventoryRecordFun();
    },
  },
  created() {
    let that = this;
    that.searchItem.planDtStartDt = that.dateExt.getDay(-1);
    that.defaulttimeList[0] = that.searchItem.planDtStartDt;
    that.searchItem.planDtEndDt = that.dateExt.getDay(1);
    that.defaulttimeList[1] = that.searchItem.planDtEndDt;
    that.GetWorkShopTypesAllFun();
  },
  mounted() {
    this.$nextTick(() => {
      this.topTable =
        document.getElementById("TableBasicBox").offsetHeight - 40;
      this.bottomTable =
        document.getElementById("ExtattrbuteTableBox").offsetHeight - 80;
    });
    this.getData();
  },
};
</script>
<style scoped lang="less">
.operation {
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
  padding: 0.5rem 1rem;
  flex: 1;
  overflow: hidden;
  position: relative;

  .top,
  .bottom {
    flex: 1;
    margin-bottom: 0.625rem;
    display: flex;
    flex-direction: column;
    header {
      margin-bottom: 0.625rem;
    }
    h3 {
      display: flex;
      align-items: center;
      .tip {
        height: 5px;
        width: 5px;
        background-color: #009999;
        display: inline-block;
        margin-right: 10px;
      }
    }
    section {
      display: flex;
      flex-direction: column;
      flex: 1;
      .table-basic-box {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-height: 0;

        .ant-table-wrapper {
          flex: 3;
        }

        .table-paging {
          margin: 0.3125rem 0;
          display: flex;
          justify-content: flex-end;
        }
      }
    }
  }
  .bottom {
    flex: 1;
    margin-bottom: 0;
    header {
      display: flex;
      align-items: center;
      width: 100%;
      margin-bottom: 0.625rem;
      .tab-box {
        border: 1px solid #bbb5b5;
        display: flex;
        align-items: center;
        li {
          padding: 0.3125rem 0.9375rem;
          cursor: pointer;
        }
        li:first-child {
          border-right: 1px solid #bbb5b5;
        }
        .active-class {
          background-color: #20b2aa;
          color: #ffffff;
        }
      }
      .operation-search {
        flex: 1;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
